<template>
  <div>
    <el-dialog
      :title="ruleForm.id == '' ? '添加告警模板' : '编辑告警模板'"
      :visible.sync="addEditIsDialog_c"
      @close="resetForm('formName')"
      width="800px"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="formName"
        label-width="120px"
        class="demo-ruleForm"
        :class="{ 'form-rules-show': seeType }"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="机构名称：" prop="companyId">
              <el-select
                v-model="ruleForm.companyId"
                placeholder="请选择机构名称"
                clearable
                :disabled="seeType || userType == 2"
              >
                <el-option
                  v-for="item in companyArray"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="模板名称：" prop="name">
              <el-input
                v-model="ruleForm.name"
                placeholder="请输入模板名称"
                clearable
                maxlength="50"
                show-word-limit
                :disabled="seeType"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button type="primary" @click="submitForm('formName')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
// import { getModelAlarmSettings, saveModelAlarmSettings } from "@/api/alarm";
import { getCompanyInfos } from "@/api/company";

export default {
  props: {
    addEditId: {
      type: String,
      default: "",
    },
    addEditStatus: {
      type: String,
      default: "add",
    },
    addEditIsDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: "",
        companyId: "",
        name: "",
      },
      rules: {
        // 表单规则
        companyId: [
          { required: true, message: "请选择机构名称", trigger: "change" },
        ],
        name: [{ required: true, message: "请输入模板名称", trigger: "blur" }],
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      companyArray: [], // 机构数组
      userType: "", // 账号类型（0超级管理员，2组织管理员，3一般管理员）
      companyId: "", // 机构id
    };
  },
  watch: {
    // addEditIsDialog(val) {
    //   this.addEditIsDialog_c = val;
    //   if (this.addEditIsDialog_c) {
    //     // 添加
    //     if (this.addEditStatus == "add") {
    //       this.ruleForm.id = "";
    //       this.seeType = false;
    //     }

    //     // 编辑
    //     if (this.addEditStatus == "edit") {
    //       this.ruleForm.id = this.addEditId;
    //       this.seeType = false;
    //       this.getDetail();
    //     }

    //     // 查看
    //     if (this.addEditStatus == "see") {
    //       this.ruleForm.id = this.addEditId;
    //       this.seeType = true;
    //       this.getDetail();
    //     }

    //     // 获取机构列表
    //     this.getCompanyList();
    //   }
    // },
  },
  methods: {
    // 详情
    getDetail() {
      getModelAlarmSettings({ id: this.addEditId }).then((res) => {
        if (res.code == 200) {
          this.ruleForm = res.data;
        }
      });
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveModelAlarmSettings(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });

              // 关闭弹窗
              this.$parent.closeAddEdit();
              // 刷新列表
              this.$parent.onSubmit();
            }
          });
        }
      });
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$parent.closeAddEdit();
    },
    // 获取机构列表
    getCompanyList() {
      getCompanyInfos().then((res) => {
        if (res.code == 200) {
          this.companyArray = res.data;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;

  .el-input-number {
    width: 100%;
  }
}
</style>
